package com.wnhz.work.yz.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.base.BaseRVAdapter;
import com.wnhz.work.yz.base.BaseViewHolder;
import com.wnhz.work.yz.bean.BiaoZhunBean;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import fm.jiecao.jcvideoplayer_lib.JCVideoPlayerStandard;

public class YanShouBiao2ZhunActivity extends BaseActivity implements View.OnClickListener {
    private RecyclerView recycler;
    private EditText ll_search2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_yan_shou_quan_biao_zhun);
        TextView title = (TextView) findViewById(R.id.view_main_middle_title);
        if ("1".equals(getIntent().getStringExtra("type"))) {
            title.setText("全装标准");
        } else if ("3".equals(getIntent().getStringExtra("type"))) {
            title.setText("厨房标准");
        } else if ("2".equals(getIntent().getStringExtra("type"))) {
            title.setText("浴室标准");
        }
        ll_search2 = (EditText) findViewById(R.id.ll_search2);

        findViewById(R.id.btn_main_left).setOnClickListener(this);
        recycler = (RecyclerView) findViewById(R.id.recycler);
        recycler.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        upLoad();
    }

    private void upLoad() {
        Map<String, Object> map = new HashMap<>();
        map.put("type", getIntent().getStringExtra("type"));
        showDialog();
        Set keys = map.keySet();
        if (keys != null) {
            Iterator iterator = keys.iterator();
            while (iterator.hasNext()) {
                Object key = iterator.next();
                Object value = map.get(key);
                Log.e("----" + key, "" + value + "\n");
            }
        }
        XUtil.Post(Url.DECORATE_STANDARDFILE, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i("----", "onSuccess:= " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    Log.e("----", "onSuccess:= " + info);
                    if ("1".equals(res)) {
                        Gson gson = new Gson();
                        BiaoZhunBean bean = gson.fromJson(result, BiaoZhunBean.class);
                        List<BiaoZhunBean.InfoEntity> info1 = bean.getInfo();
                        setData(info1);


                    } else {
                        if ("-1".equals(res)) {
                            info = "请重新登录";
                        }
                        MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                closeDialog();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                closeDialog();
                ex.printStackTrace();
            }
        });


    }

    private void setData(final List<BiaoZhunBean.InfoEntity> info1) {
        ll_search2.setVisibility(View.VISIBLE);

        recycler.setAdapter(new BaseRVAdapter(this, info1) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.item_biaozhun;
            }

            @Override
            public void onBind(BaseViewHolder holder, final int position) {

                //标题内容
                holder.setTextView(R.id.title, info1.get(position).getTitle());
                holder.setTextView(R.id.content, info1.get(position).getDetail());
                if ("".equals(info1.get(position).getDetail())) {
                    holder.getView(R.id.content).setVisibility(View.GONE);
                } else {
                    holder.getView(R.id.content).setVisibility(View.VISIBLE);
                    holder.setTextView(R.id.content, info1.get(position).getDetail());
                }
                //视频
                RecyclerView video = holder.getView(R.id.video);
                video.setLayoutManager(new LinearLayoutManager(YanShouBiao2ZhunActivity.this, LinearLayoutManager.VERTICAL, false));
                final List<BiaoZhunBean.InfoEntity.VideoEntity> video1 = info1.get(position).getVideo();
                video.setAdapter(new BaseRVAdapter(YanShouBiao2ZhunActivity.this, video1) {
                    @Override
                    public int getLayoutId(int viewType) {
                        return R.layout.video;
                    }

                    @Override
                    public void onBind(BaseViewHolder holder, int position2) {
                        JCVideoPlayerStandard jcVideoPlayerStandard = holder.getView(R.id.jc);
                        jcVideoPlayerStandard.setUp(video1.get(position2).getVideo_url()
                                , JCVideoPlayerStandard.SCREEN_LAYOUT_NORMAL, info1.get(position).getTitle());
                        com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(video1.get(position2).getVideo_img(),
                                jcVideoPlayerStandard.thumbImageView);

                    }
                });
                //图片
                final List<String> img = info1.get(position).getImg();
                LinearLayout status1 = holder.getView(R.id.status1);
                LinearLayout status2 = holder.getView(R.id.status2);
                LinearLayout status3 = holder.getView(R.id.status3);
                ImageView img1 = holder.getImageView(R.id.img1);
                ImageView img2_1 = holder.getImageView(R.id.img2_1);
                ImageView img2_2 = holder.getImageView(R.id.img2_2);
                RecyclerView img_recycler = holder.getView(R.id.img_recycler);
                switch (img.size()) {
                    case 0:
                        status1.setVisibility(View.GONE);
                        status2.setVisibility(View.GONE);
                        status3.setVisibility(View.GONE);


                        break;
                    case 1:
                        status1.setVisibility(View.VISIBLE);
                        status2.setVisibility(View.GONE);
                        status3.setVisibility(View.GONE);
                        com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(img.get(0),
                                img1);
                        img1.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Intent intent = new Intent(YanShouBiao2ZhunActivity.this, ShowImageDetail.class);
                                intent.putStringArrayListExtra("paths", (ArrayList<String>) img);
                                intent.putExtra("index", 0);
                                startActivity(intent);
                            }
                        });
                        break;
                    case 2:
                        status1.setVisibility(View.GONE);
                        status2.setVisibility(View.VISIBLE);
                        status3.setVisibility(View.GONE);
                        com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(img.get(0),
                                img2_1);
                        com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(img.get(1),
                                img2_2);
                        img2_1.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Intent intent = new Intent(YanShouBiao2ZhunActivity.this, ShowImageDetail.class);
                                intent.putStringArrayListExtra("paths", (ArrayList<String>) img);
                                intent.putExtra("index", 0);
                                startActivity(intent);
                            }
                        });
                        img2_2.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Intent intent = new Intent(YanShouBiao2ZhunActivity.this, ShowImageDetail.class);
                                intent.putStringArrayListExtra("paths", (ArrayList<String>) img);
                                intent.putExtra("index", 1);
                                startActivity(intent);
                            }
                        });
                        break;
                    default:
                        status1.setVisibility(View.GONE);
                        status2.setVisibility(View.GONE);
                        status3.setVisibility(View.VISIBLE);
                        img_recycler.setLayoutManager(new GridLayoutManager(YanShouBiao2ZhunActivity.this, 3, LinearLayoutManager.VERTICAL, false));
                        img_recycler.setAdapter(new BaseRVAdapter(YanShouBiao2ZhunActivity.this, img) {
                            @Override
                            public int getLayoutId(int viewType) {
                                return R.layout.item_photo;
                            }

                            @Override
                            public void onBind(BaseViewHolder holder, final int position) {
                                holder.getImageView(R.id.img).setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        Intent intent = new Intent(YanShouBiao2ZhunActivity.this, ShowImageDetail.class);
                                        intent.putStringArrayListExtra("paths", (ArrayList<String>) img);
                                        intent.putExtra("index", position);
                                        startActivity(intent);
                                    }
                                });
                                ImageLoader.getInstance().displayImage(img.get(position), holder.getImageView(R.id.img));

                            }

                        });

                        break;
                }
                //子类
            final    RecyclerView recycler = holder.getView(R.id.recycler);
                ll_search2.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                    }

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {
                        for (int i = 0; i < info1.size(); i++) {
                            if (info1.get(i).getTitle().contains(ll_search2.getText().toString().trim())) {
                                recycler.scrollToPosition(i);
                                return;
                            } else {
                                for (int j = 0; j < info1.get(i).getCate().size(); j++) {
                                    if (info1.get(i).getCate().get(j).getTitle().contains(ll_search2.getText().toString().trim())) {
                                        recycler.scrollToPosition(i);
                                        return;
                                    }
                                }
                            }
                        }
                    }

                    @Override
                    public void afterTextChanged(Editable s) {

                    }
                });

                recycler.setLayoutManager(new LinearLayoutManager(YanShouBiao2ZhunActivity.this, LinearLayoutManager.VERTICAL, false));
                final List<BiaoZhunBean.InfoEntity.CateEntity> cate = info1.get(position).getCate();
                recycler.setAdapter(new BaseRVAdapter(YanShouBiao2ZhunActivity.this, cate) {
                    @Override
                    public int getLayoutId(int viewType) {
                        return R.layout.item_biaozhun2;
                    }

                    @Override
                    public void onBind(BaseViewHolder holder, final int position) {
                        //标题内容
                        holder.setTextView(R.id.title, cate.get(position).getTitle());
                        holder.setTextView(R.id.content, cate.get(position).getDetail());
                        //视频
                        RecyclerView video = holder.getView(R.id.video);
                        video.setLayoutManager(new LinearLayoutManager(YanShouBiao2ZhunActivity.this, LinearLayoutManager.VERTICAL, false));
                        final List<BiaoZhunBean.InfoEntity.CateEntity.VideoEntity> video1 = cate.get(position).getVideo();
                        video.setAdapter(new BaseRVAdapter(YanShouBiao2ZhunActivity.this, video1) {
                            @Override
                            public int getLayoutId(int viewType) {
                                return R.layout.video;
                            }

                            @Override
                            public void onBind(BaseViewHolder holder, int position2) {
                                JCVideoPlayerStandard jcVideoPlayerStandard = holder.getView(R.id.jc);
                                jcVideoPlayerStandard.setUp(video1.get(position2).getVideo_url()
                                        , JCVideoPlayerStandard.SCREEN_LAYOUT_NORMAL, cate.get(position).getTitle());
                                com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(video1.get(position2).getVideo_img(),
                                        jcVideoPlayerStandard.thumbImageView);

                            }
                        });
                        //图片
                        final List<String> img = cate.get(position).getImg();
                        LinearLayout status1 = holder.getView(R.id.status1);
                        LinearLayout status2 = holder.getView(R.id.status2);
                        LinearLayout status3 = holder.getView(R.id.status3);
                        ImageView img1 = holder.getImageView(R.id.img1);
                        ImageView img2_1 = holder.getImageView(R.id.img2_1);
                        ImageView img2_2 = holder.getImageView(R.id.img2_2);
                        RecyclerView img_recycler = holder.getView(R.id.img_recycler);
                        switch (img.size()) {
                            case 0:
                                status1.setVisibility(View.GONE);
                                status2.setVisibility(View.GONE);
                                status3.setVisibility(View.GONE);


                                break;
                            case 1:
                                status1.setVisibility(View.VISIBLE);
                                status2.setVisibility(View.GONE);
                                status3.setVisibility(View.GONE);
                                com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(img.get(0),
                                        img1);
                                img1.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        Intent intent = new Intent(YanShouBiao2ZhunActivity.this, ShowImageDetail.class);
                                        intent.putStringArrayListExtra("paths", (ArrayList<String>) img);
                                        intent.putExtra("index", 0);
                                        startActivity(intent);
                                    }
                                });
                                break;
                            case 2:
                                status1.setVisibility(View.GONE);
                                status2.setVisibility(View.VISIBLE);
                                status3.setVisibility(View.GONE);
                                com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(img.get(0),
                                        img2_1);
                                com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(img.get(1),
                                        img2_2);
                                img2_1.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        Intent intent = new Intent(YanShouBiao2ZhunActivity.this, ShowImageDetail.class);
                                        intent.putStringArrayListExtra("paths", (ArrayList<String>) img);
                                        intent.putExtra("index", 0);
                                        startActivity(intent);
                                    }
                                });
                                img2_2.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        Intent intent = new Intent(YanShouBiao2ZhunActivity.this, ShowImageDetail.class);
                                        intent.putStringArrayListExtra("paths", (ArrayList<String>) img);
                                        intent.putExtra("index", 1);
                                        startActivity(intent);
                                    }
                                });
                                break;
                            default:
                                status1.setVisibility(View.GONE);
                                status2.setVisibility(View.GONE);
                                status3.setVisibility(View.VISIBLE);
                                img_recycler.setLayoutManager(new GridLayoutManager(YanShouBiao2ZhunActivity.this, 3, LinearLayoutManager.VERTICAL, false));
                                img_recycler.setAdapter(new BaseRVAdapter(YanShouBiao2ZhunActivity.this, img) {
                                    @Override
                                    public int getLayoutId(int viewType) {
                                        return R.layout.item_photo;
                                    }

                                    @Override
                                    public void onBind(BaseViewHolder holder, final int position) {
                                        holder.getImageView(R.id.img).setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {
                                                Intent intent = new Intent(YanShouBiao2ZhunActivity.this, ShowImageDetail.class);
                                                intent.putStringArrayListExtra("paths", (ArrayList<String>) img);
                                                intent.putExtra("index", position);
                                                startActivity(intent);
                                            }
                                        });
                                        ImageLoader.getInstance().displayImage(img.get(position), holder.getImageView(R.id.img));

                                    }

                                });

                                break;
                        }

                        RecyclerView recycler = holder.getView(R.id.recycler);
                        recycler.setLayoutManager(new LinearLayoutManager(YanShouBiao2ZhunActivity.this, LinearLayoutManager.VERTICAL, false));
                        final List<BiaoZhunBean.InfoEntity.CateEntity.ChildEntity> child = cate.get(position).getChild();
                        recycler.setAdapter(new BaseRVAdapter(YanShouBiao2ZhunActivity.this, child) {
                            @Override
                            public int getLayoutId(int viewType) {
                                return R.layout.item_biaozhun3;
                            }

                            @Override
                            public void onBind(BaseViewHolder holder, final int position) {

                                //标题内容
                                holder.setTextView(R.id.title, child.get(position).getTitle());
                                holder.setTextView(R.id.content, child.get(position).getDetail());
                                //视频
                                RecyclerView video = holder.getView(R.id.video);
                                video.setLayoutManager(new LinearLayoutManager(YanShouBiao2ZhunActivity.this, LinearLayoutManager.VERTICAL, false));
                                final List<BiaoZhunBean.InfoEntity.CateEntity.ChildEntity.VideoEntity> video1 = child.get(position).getVideo();
                                video.setAdapter(new BaseRVAdapter(YanShouBiao2ZhunActivity.this, video1) {
                                    @Override
                                    public int getLayoutId(int viewType) {
                                        return R.layout.video;
                                    }

                                    @Override
                                    public void onBind(BaseViewHolder holder, int position2) {
                                        JCVideoPlayerStandard jcVideoPlayerStandard = holder.getView(R.id.jc);
                                        jcVideoPlayerStandard.setUp(video1.get(position2).getVideo_url()
                                                , JCVideoPlayerStandard.SCREEN_LAYOUT_NORMAL, child.get(position).getTitle());
                                        com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(video1.get(position2).getVideo_img(),
                                                jcVideoPlayerStandard.thumbImageView);

                                    }
                                });
                                //图片
                                final List<String> img = child.get(position).getImg();
                                LinearLayout status1 = holder.getView(R.id.status1);
                                LinearLayout status2 = holder.getView(R.id.status2);
                                LinearLayout status3 = holder.getView(R.id.status3);
                                ImageView img1 = holder.getImageView(R.id.img1);
                                ImageView img2_1 = holder.getImageView(R.id.img2_1);
                                ImageView img2_2 = holder.getImageView(R.id.img2_2);
                                RecyclerView img_recycler = holder.getView(R.id.img_recycler);
                                switch (img.size()) {
                                    case 0:
                                        status1.setVisibility(View.GONE);
                                        status2.setVisibility(View.GONE);
                                        status3.setVisibility(View.GONE);


                                        break;
                                    case 1:
                                        status1.setVisibility(View.VISIBLE);
                                        status2.setVisibility(View.GONE);
                                        status3.setVisibility(View.GONE);
                                        com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(img.get(0),
                                                img1);
                                        img1.setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {
                                                Intent intent = new Intent(YanShouBiao2ZhunActivity.this, ShowImageDetail.class);
                                                intent.putStringArrayListExtra("paths", (ArrayList<String>) img);
                                                intent.putExtra("index", 0);
                                                startActivity(intent);
                                            }
                                        });
                                        break;
                                    case 2:
                                        status1.setVisibility(View.GONE);
                                        status2.setVisibility(View.VISIBLE);
                                        status3.setVisibility(View.GONE);
                                        com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(img.get(0),
                                                img2_1);
                                        com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(img.get(1),
                                                img2_2);
                                        img2_1.setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {
                                                Intent intent = new Intent(YanShouBiao2ZhunActivity.this, ShowImageDetail.class);
                                                intent.putStringArrayListExtra("paths", (ArrayList<String>) img);
                                                intent.putExtra("index", 0);
                                                startActivity(intent);
                                            }
                                        });
                                        img2_2.setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {
                                                Intent intent = new Intent(YanShouBiao2ZhunActivity.this, ShowImageDetail.class);
                                                intent.putStringArrayListExtra("paths", (ArrayList<String>) img);
                                                intent.putExtra("index", 1);
                                                startActivity(intent);
                                            }
                                        });
                                        break;
                                    default:
                                        status1.setVisibility(View.GONE);
                                        status2.setVisibility(View.GONE);
                                        status3.setVisibility(View.VISIBLE);
                                        img_recycler.setLayoutManager(new GridLayoutManager(YanShouBiao2ZhunActivity.this, 3, LinearLayoutManager.VERTICAL, false));
                                        img_recycler.setAdapter(new BaseRVAdapter(YanShouBiao2ZhunActivity.this, img) {
                                            @Override
                                            public int getLayoutId(int viewType) {
                                                return R.layout.item_photo;
                                            }

                                            @Override
                                            public void onBind(BaseViewHolder holder, final int position) {
                                                holder.getImageView(R.id.img).setOnClickListener(new View.OnClickListener() {
                                                    @Override
                                                    public void onClick(View v) {
                                                        Intent intent = new Intent(YanShouBiao2ZhunActivity.this, ShowImageDetail.class);
                                                        intent.putStringArrayListExtra("paths", (ArrayList<String>) img);
                                                        intent.putExtra("index", position);
                                                        startActivity(intent);
                                                    }
                                                });
                                                ImageLoader.getInstance().displayImage(img.get(position), holder.getImageView(R.id.img));

                                            }

                                        });

                                        break;
                                }
                            }


                        });

                    }
                });

            }

        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;
        }
    }
}
